package com.example.administrator.test2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.os.Build;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Date;
import com.example.administrator.test2.time;
import com.example.administrator.test2.car;

import junit.runner.Version;

/**
 * Created by Administrator on 2017-03-29.
 */
public class DatabaseHandler1 extends SQLiteOpenHelper {
    private Context mcontext;
    private time t2;
    private DatabaseHandler1 data;
    private static final String DATABASE_NAME="Test";
    private static final String TABLE_NAME="car1";
    private static final String KEY_NUM="number";
    private static final String KEY_TIMEIN="timein";
    private static final String KEY_TIMEOUT="timeout";
    private static final String KEY_TEXTNUM="textnumber";
    private static final String CREATE_TABLE="create table "+ TABLE_NAME+"("
                                              + KEY_NUM +" text primary key ,"
                                              + KEY_TIMEIN +" text ,"
                                              + KEY_TIMEOUT +" text,"
                                              + KEY_TEXTNUM +" integer)";
    private final static int VERSION = 1;
    private SQLiteDatabase db;
    //SQLiteOpenHelper子类必须要的一个构造函数
    public DatabaseHandler1(Context context, String name, CursorFactory factory, int version) {
        //必须通过super 调用父类的构造函数
        super(context, name, factory, version);
        mcontext=context;
    }

    //数据库的构造函数，传递三个参数的
    //public DatabaseHandler1(Context context, String name, int version){
       // this(context, name, null, version);
   // }
    // 回调函数，第一次创建时才会调用此函数，创建一个数据库
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
        Toast.makeText(mcontext,"Create Succeeded",Toast.LENGTH_SHORT).show();

    }

    //回调函数，当你构造DBHelper的传递的Version与之前的Version调用此函数
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        System.out.println("update Database");

    }
    public void adddata(car c1,DatabaseHandler1 dbHelper){//获取一个可读写的数据库,db为操作对象,由它调用操作方法
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues() ;
        values.put("number",c1.getNumber());
        values.put(KEY_TIMEIN,c1.gettimein());
        values.put("textnumber",10);
        db.insert(TABLE_NAME,null,values);//插入一条信息到数据库
        Cursor cursor = db.query("car1", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String number = cursor.getString(cursor.getColumnIndex("number"));
            String timein = cursor.getString(cursor.getColumnIndex("timein"));
            String timeout = cursor.getString(cursor.getColumnIndex("timeout"));
            Log.d("in", "number:" + number);
            Log.d("in", "time in:" + timein);
            Log.d("in", "time out:" + timeout);
        }
        db.close();
    }
    public void updateContact(car c1,DatabaseHandler1 dbHelper) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("car1", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String number = cursor.getString(cursor.getColumnIndex("number"));
            String timein = cursor.getString(cursor.getColumnIndex("timein"));
            String timeout = cursor.getString(cursor.getColumnIndex("timeout"));
            Log.d("in", "number:" + number);
            Log.d("in", "time in:" + timein);
            Log.d("in", "time out:" + timeout);
        }
        ContentValues values = new ContentValues();
        values.put(KEY_TIMEOUT, c1.gettimeout());
        values.put(KEY_TEXTNUM, 5);
        db.update(TABLE_NAME, values, "KEY_NUM=?", new String[]{c1.getNumber()});
        while (cursor.moveToNext()) {
            String number = cursor.getString(cursor.getColumnIndex("number"));
            String timein = cursor.getString(cursor.getColumnIndex("timein"));
            String timeout = cursor.getString(cursor.getColumnIndex("timeout"));
            Log.d("in", "number:" + number);
            Log.d("in", "time in:" + timein);
            Log.d("in", "time out:" + timeout);
        }
        db.close();
    }
    public car getCar(String number,DatabaseHandler1 dbHelper){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        Cursor cursor=db.query(TABLE_NAME,new String[]{KEY_NUM,KEY_TIMEIN,KEY_TIMEOUT},KEY_NUM+"=?",new String[]{number},null,null,null,null);
        car c1=null;
        if (cursor.moveToFirst()){
            c1=new car(cursor.getString(0),cursor.getString(1),cursor.getString(2));
        }
        return c1;
    }
}
